package com.example.retrofitdemo2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;



import okhttp3.FormBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private static final String TAG="tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView(){
        Button button=findViewById(R.id.btn_send_get);
        Button btnSendPost=findViewById(R.id.btn_send_post);
        Button btnGetRxJava=findViewById(R.id.btn_send_get_rxjava);
        RecyclerView recyclerView=findViewById(R.id.btn_city_list);
        btnGetRxJava.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.weatherRequest.requestCityListRX("8cf90bdd00d8ff3b42af97f57977b8ba")
                        //线程调度器，开启子线程，将事件加入到子线程中并执行
                        .subscribeOn(Schedulers.io())
                        //RxAndroid的库，线程切换至UI线程
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Action1<BaseListRequest<CityIteam>>() {
                                       @Override
                                       public void call(BaseListRequest<CityIteam> cityIteamBaseListRequest) {
                                           for (CityIteam city:cityIteamBaseListRequest.getResult()) {
                                               Log.d(TAG,"结果为："+city.getDistrict());
                                           }
                                       }
                                   });
            }
        });
        LinearLayoutManager llm=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(llm);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.weatherRequest.requestList("8cf90bdd00d8ff3b42af97f57977b8ba")
                        .enqueue(new Callback<BaseListRequest<CityIteam>>() {
                            @Override
                            public void onResponse(Call<BaseListRequest<CityIteam>> call, Response<BaseListRequest<CityIteam>> response) {
                                CityIteam[] result=response.body().getResult();
                                for (int i = 0; i <result.length ; i++) {
                                    Log.d(TAG,"id="+result[i].getId()+
                                            "province="+result[i].getProvince()
                                    +"city="+result[i].getCity()
                                    +"district="+result[i].getDistrict());
                                }
                            }
                            @Override
                            public void onFailure(Call<BaseListRequest<CityIteam>> call, Throwable t) {

                            }
                        });
            }
        });
        btnSendPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FormBody formBody=new FormBody.Builder()
                        .add("key","e9cc240a2e1eac66e7ca4accd848d3bb")
                        .build();
                BaseApplication.weatherRequest.requestPost(formBody)
                        .enqueue(new Callback<BaseListRequest<CityIteam>>() {
                    @Override
                    public void onResponse(Call<BaseListRequest<CityIteam>> call, Response<BaseListRequest<CityIteam>> response) {
                        CityIteam[] result=response.body().getResult();
                        List<CityIteam> list=new ArrayList<>();
                        for (int i = 0; i <result.length ; i++) {
                            Log.d(TAG,"id="+result[i].getId()+
                                    "province="+result[i].getProvince()
                                    +"city="+result[i].getCity()
                                    +"district="+result[i].getDistrict());
                            CityIteam cityIteam=new CityIteam();
                            if(i==0){
                                cityIteam.setCity(result[i].getCity());
                                list.add(cityIteam);
                            }else if (!result[i-1].getCity().equals(result[i].getCity())) {
                                cityIteam.setCity(result[i].getCity());
                                list.add(cityIteam);
                            }
                        }
                        MyAdapter adapter=new MyAdapter(MainActivity.this);
                        adapter.setList(list);
                        recyclerView.setAdapter(adapter);

                    }

                    @Override
                    public void onFailure(Call<BaseListRequest<CityIteam>> call, Throwable t) {

                    }
                });

            }
        });

    }
}